
package com.yimixian.app.rest.api;

import android.text.TextUtils;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.Address;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.model.SortGoodsItem;
import com.yimixian.app.util.SharedPreferencesHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.client.Response;

/**
 * Created by szy on 2016/1/20.
 */
public class HuanGouKeyAPI extends YmxBaseAPI<List<GoodsItem>> {

    public static final String HUAN_GOU_KEY = "/goods/gift_good";
    public List<SortGoodsItem> goodsItems;
    /**
     * @param callback
     */
    public HuanGouKeyAPI(Callback<Response> callback) {
        super(callback);
    }

    @Override
    public String getRelativeURL() {
        return HUAN_GOU_KEY;
    }

    public void getHuanGouData(String group_id) {
        Address address = SharedPreferencesHelper.getAddress(SharePreferenceKeys.CURRENT_ADDRESS);
        String storeIdStr = "";
        if (address != null) {
            storeIdStr = address.storeId;
        }
        String token = DataManager.getInstance().get(CacheKeys.TOKEN);
        int storeId = 0;
        if(!TextUtils.isEmpty(storeIdStr)) {
            storeId = Integer.parseInt(storeIdStr);
        }
        if(TextUtils.isEmpty(token)) {
            token = "";
        }
//        mYmxRestService.getSearchKeyResult(storeId,token,keyWord,page,mCallback);
        mYmxRestService.getHuanGouData(address.longitude, address.latitude, storeIdStr, token, group_id, mCallback);
    }


    @Override
    public List<GoodsItem> parseJson(JSONObject jsonObject) {
        JSONArray array = jsonObject.optJSONArray("goods");
        List<GoodsItem> mGoodsItems = new ArrayList<>();
        if (array != null){
            for (int l = 0; l < array.length(); l++){
                JSONObject object1 = array.optJSONObject(l);
                GoodsItem goodsItem = new GoodsItem();
                goodsItem.id = object1.optInt("id");
                goodsItem.name = object1.optString("name");
                if (!StringUtils.isEmpty(object1.optString("img_ratio"))){
                    goodsItem.imgRatio = object1.optInt("img_ratio");
                }
                goodsItem.img = object1.optString("img");
                goodsItem.promotionImg = object1
                        .optString("promotion_img");
                goodsItem.descUrl = object1.optString("desc_url");
                goodsItem.unitDesc = object1.optString("unit_desc");
                goodsItem.saleStatus = object1.optString("sale_status");
                goodsItem.unitPrice = object1.optString("unit_price");
                goodsItem.stockCount = object1.optString("stock_count");
                goodsItem.start_time = object1.optLong("start_time");
                goodsItem.unitOldPrice = object1
                        .optString("unit_old_price");
                goodsItem.shouldBuyCount = object1
                        .optInt("should_buy_count");
                goodsItem.isBuyingGroup = object1
                        .optInt("is_buying_group");
                goodsItem.buyingGroupCount = object1
                        .optInt("buying_group_count");
                goodsItem.buyingGroupPrice = object1
                        .optInt("buying_group_price");
                goodsItem.countZeroReason = object1.optString("count_zero_reason");
                mGoodsItems.add(goodsItem);
            }
        }

        return mGoodsItems;

    }

    public List<SortGoodsItem> translateArray(List<GoodsItem> goodsItemList){
        goodsItems = new ArrayList<>();

        if (goodsItemList == null) {
            return goodsItems;
        }
        List<GoodsItem> items = goodsItemList;
        int size = items.size();
        //添加section后面的item
        for (int j = 0; j < size; j++){
            SortGoodsItem sortGoodsItem = new SortGoodsItem(SortGoodsItem.ITEM);
            if (2 * j <= size - 1) {
                sortGoodsItem.leftGoodsItem = items.get(2 * j);
                if (2 * j + 1 <= size - 1){
                    sortGoodsItem.RightGoodsItem = items.get(2 * j + 1);
                }
                goodsItems.add(sortGoodsItem);
            }

        }
        return goodsItems;
    }
}
